home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: storing address of member functions, HELP!
- Date: 18 Jan 1996 19:57:33 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4dm8nd$q2d@colossus.holonet.net>
- References: <4djbj4$5nu@news-rocq.inria.fr> <4dju3b$ehi@gateway.comsearch.com>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Thuan Nguyen (thnguyen@comsearch.com) wrote:
- : Sophie.Cluet@inria.fr (Sophie Cluet) wrote:
-
- [some deletions in the following]
-
- : > Class nary_op
- : > {...
- : > int evaluate(); // evaluate the parameters and invoke
- : > // the below stored function (code)
- : > int (nary_op::*code)(); // address of the evaluation function
- : > int build_list(); // one such evaluation fonction
- : > ...}
- : > Class struct_op: public nary_op
- : > {char ** names; // added stored information
- : > int build_struct(); // an evaluation function
- : > // that uses the above names
- : > ...}
- : >Now, the problem. The compiler accepts neither of the following
- : >instructions on a nary_op:
- : >
- : > code=&struct_op::build_struct;
- : > or
- : > code=(int (nary_op::*)())&struct_op::build_struct;
- : >
- [...]
- : Let's me try. The scope of the function is the same as the scope of the object
- : itself. In code = &struct_op::build_struct; the object doesnt exist.
- : How about declaring the build_struct function static.
-
- No. &struct_op::build_struct does not need an object, so your
- suggestion is based on an incorrect assumption. Static members
- would change the picture, indeed, but I don't think they're what
- we want here.
-
- There simply is NO WAY to store a struct_op::* in a nary_op::*
- variable. Casting is not allowed for this kind of pointer.
-
- Anyway, if you think about it, you really wouldn't want evaluate(),
- a base-class member, to access build_struct(), a derived-class
- member, even via pointer. The only time downward access like that
- makes OO sense (or is possible in C++) is if the function is virtual.
-
- If your design is otherwise sound, I think virtual functions
- may be what you're looking for here. Perhaps build_struct
- performs the same function (but differently) as some base-class
- member which you already have; if so, declare that member virtual
- and rename build_struct to override this function. Or else,
- just declare a dummy virtual int build_struct() in your base
- class. (The latter is not as good from an OO standpoint.) Then
- code = &nary_op::build_struct; // does what you want!
-
- Here's a code snippet that illustrates my point:
-
- class B { public: virtual void foo() { cout << "base\n"; } };
- class D : public B { public: void foo() { cout << "der\n"; } };
- int main() {
- D d;
- void (B::*ptr)() = &B::foo;
- (d.*ptr)(); // prints "der"
- };
- --
- Russell Blackadar, russell@mdli.com
-